/*	
	stencil - action script 3 isometric game engine.

	http://code.google.com/p/stencilas3/
	
	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.engine.display.renderers 
{
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import stencil.core.engine.display.BitmapObject;
	import stencil.core.engine.display.IDisplayObject;
	import stencil.core.engine.IGrid;
	import stencil.core.engine.ITileSection;
	import stencil.core.engine.MapNode;
	import stencil.core.engine.Position;
	import stencil.core.engine.TileSettings;
	
	/**
	 * Draws tile sections in isometric fashion. 
	 * NOTICE: x position of tile section in pixels should be adjusted by subtracting tileDisplayObject.width/2
	 * 
	 * @author Max Zhuravkov
	 */
	public class IsometricTileRender implements ITileRender {
		
		protected static const ALPHA_BITMAP:BitmapData = new BitmapData(800, 800, false, 0xffffff);
		
		private var _tileSettings:TileSettings;
		
		private var _grid:IGrid;
		
		//------------------------------------------------------------------------------------------------
		// Constructor
		//------------------------------------------------------------------------------------------------
		
		public function IsometricTileRender() {
			
		}
		
		//------------------------------------------------------------------------------------------------
		// Public methods
		//------------------------------------------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		public function setup(grid:IGrid, tileSettings:TileSettings):void {
			this._grid = grid;
			this._tileSettings = tileSettings;
		}
		
		/**
		 * @inheritDoc
		 */
		public function createTiles(section:ITileSection):IDisplayObject {
			var rect:Rectangle =  section.rect;
			var bitmapWidth:Number = this.tileSettings.tileWidth*rect.width*this.grid.cellWidth;
			var bitmapHeight:Number = this.tileSettings.tileHeight*rect.height*this.grid.cellHeight;
			
			var bitmapData:BitmapData = this.allocateBitmapData(bitmapWidth, bitmapHeight);
			var object:BitmapObject = new BitmapObject(bitmapData, "never", false);
			
			object.id = "secton" + section.id;
			object.flagTile = true;
			object.flagStatic = true;
			object.flagBackground = true;
			
			return object;
		}
		
		protected function allocateBitmapData(width:int, height:int):BitmapData {
			return new BitmapData(width, height, true, 0x00000000);
		}
		
		/**
		 * @inheritDoc
		 */
		public function drawTiles(section:ITileSection, destination:IDisplayObject):void{
			var bitmap:BitmapObject = destination as BitmapObject;
			var rect:Rectangle = section.rect;
			var tiles:Array = section.tiles;
			
			if (section.tiles.length > 0) {
				var bitmapData:BitmapData = bitmap.bitmapData;
				bitmapData.lock();
				this.drawTilesToBitmap(bitmapData, rect, tiles, ALPHA_BITMAP);
				bitmapData.unlock();
			}
		}
		
		/**
		 * @inheritDoc
		 */
		public function updateTiles(section:ITileSection, displayObject:IDisplayObject, redrawFlag:Boolean):void {
			if (!redrawFlag) {
				return;
			}
			
			var bitmap:BitmapObject = displayObject as BitmapObject;
			var bitmapData:BitmapData = bitmap.bitmapData;
			
			bitmapData.fillRect(bitmapData.rect, 0x00000000);
			
			if (section.tiles.length > 0) {
				this.drawTiles(section, displayObject);
			}
		}
		
		/**
		 * @inheritDoc
		 */
		public function releaseTiles(section:ITileSection, displayObject:IDisplayObject):void {
			var o:BitmapObject = displayObject as BitmapObject;
			o.dispose();
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected properties
		//------------------------------------------------------------------------------------------------
		
		protected function get grid():IGrid {
			return this._grid;
		}
		
		protected function get tileSettings():TileSettings {
			return this._tileSettings;
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected methods
		//------------------------------------------------------------------------------------------------
		
		protected function drawTilesToBitmap(bitmapData:BitmapData, usedRect:Rectangle, tiles:Array, alphaBitmap:BitmapData):void {
			
			var cellWidth:Number = this.grid.cellWidth;
			var cellHeight:Number = this.grid.cellHeight;
			
			var point:Point = this.grid.getCellPosition(usedRect.x, usedRect.y, false);
			
			point.x = Math.floor(point.x);
			point.y = Math.floor(point.y);
			
			var tileWidth:int = this.tileSettings.tileWidth;
			var tileHeight:int = this.tileSettings.tileHeight;
			var flat:Boolean = this.tileSettings.flat;
			
			for each(var tile:MapNode in tiles) {
				
				if (tile.texture == -1 || tile.x % tileWidth != 0 || tile.y % tileHeight != 0) {
					continue;
				}
				
				var texture:BitmapData = this.tileSettings.tileSet.createTile(tile.texture) as BitmapData;
				var srcWidth:Number =  tileWidth * cellWidth;
				var srcHeight:Number = tileHeight * cellHeight;
				var sourceRect:Rectangle = new Rectangle(0, 0, srcWidth, srcHeight);
				var tilePosition:Point = this.grid.getCellPosition(tile.x, tile.y, false);
				
				var delta:Point = tilePosition.subtract(point);
				var destX:Number = delta.x + bitmapData.width/2 - texture.width/2;
				var destY:Number = delta.y;
				
				if (sourceRect.width + destX > bitmapData.width) {
					sourceRect.width = bitmapData.width - destX;
				}
				
				if (sourceRect.height + destY > bitmapData.height) {
					sourceRect.height = bitmapData.height - destY;
				}
				
				bitmapData.copyPixels(texture, texture.rect, new Point(destX, destY), alphaBitmap, new Point(0,0), true);
			}
		}
	}
	
}